package com.example.bbs.bean;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.bbs.config.HttpConstant;
import com.example.bbs.util.FileUtil;

import java.util.function.Consumer;

/**
 * MediaData 用于解析动态文本内容
 */
@SuppressWarnings("unused")
public class MediaData {

    public static final int TYPE_TEXT = 0;
    public static final int TYPE_IMAGE = 1;
    public static final int TYPE_AUDIO = 2;
    public static final int TYPE_VIDEO = 3;
    public static final String PARAM_COVER_URL = "cover_url";
    static final String LOG_TAG = MediaData.class.getSimpleName();

    private int type = 0;
    private String uri = "";
    private String text = "";
    private String params = ""; // 使用 JSONString 保存一些参数

    public MediaData() { }

    public MediaData(int type, String uri, String text, String params) {
        this.type = type;
        this.uri = uri;
        this.text = text;
        this.params = params;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public String getUri() {
        return uri;
    }

    public void setUri(String uri) {
        this.uri = uri;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getParams() {
        return params;
    }

    public void setParams(String params) {
        this.params = params;
    }

    public JSONObject fetchJsonParams() {
        JSONObject jsonObject = JSON.parseObject(params);
        if (jsonObject == null) {
            jsonObject = new JSONObject();
        }
        return jsonObject;
    }

    public void putJsonParam(String key, Object value) {
        JSONObject jsonObject = fetchJsonParams();
        jsonObject.put(key, value);
        params = jsonObject.toString();
    }

    public String fetchBaseUrl() {
        return fetchBaseUrl(type);
    }

    public static String fetchBaseUrl(int inType) {
        switch (inType) {
            case TYPE_IMAGE:
                return HttpConstant.MEDIA_IMAGE_URL;
            case TYPE_VIDEO:
                return HttpConstant.MEDIA_VIDEO_URL;
            case TYPE_AUDIO:
                return HttpConstant.MEDIA_AUDIO_URL;
        }
        return "";
    }

    public String fetchTypeName() {
        return fetchTypeName(type);
    }

    public static String fetchTypeName(int inType) {
        switch (inType) {
            case TYPE_IMAGE:
                return "image/*";
            case TYPE_VIDEO:
                return "video/*";
            case TYPE_AUDIO:
                return "audio/*";
        }
        return "";
    }

    public String fetchNameOnline() {
        return fetchNameOnline(uri);
    }

    public static String fetchNameOnline(String inUri) {
        if (!inUri.startsWith(FileUtil.UPLOADED_URL_PREFIX))
            return "";
        return inUri.substring(inUri.lastIndexOf('/') + 1); // 如果不存在，返回整个字符串
    }
}
